package com.api.shopapi.mapper;

import com.api.shopapi.entity.Checkin;
import com.api.shopapi.entity.Elderly;
import com.api.shopapi.entity.dto.CheckinDTO;
import com.api.shopapi.entity.dto.CheckinDTO1;
import com.api.shopapi.entity.dto.CheckinDTO2;
import com.api.shopapi.entity.dto.ElderlyDTO;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * @author maochuan
 */
@Repository
@Mapper
public interface CheckinMapper {
    @Insert("insert into checkin " +
            "(CheckinCode,ElderlyCode,ElderlyName,CheckinSetmealCode,CheckinSetmealName,BerthCode,BerthNumber,DurationPlan,MoneyPlan,BeginTime,EndTime," +
            "CreateTime,Type,Status,StaffCode,InstitutionCode,ContractImg,ChargeType,CheckinAutomatchCode,BeginTimePlan,EndTimePlan,Price,PriceMonth," +
            "PriceDeduction,PriceMonthDeduction,FixedFee,FixedFeeDeductionStatus,NeedVerify,DeductionStartTime,DeductionEndTime,PreferentialPrice," +
            "PreferentialEndTime,CutPrice,Remarks,AuditResult,NoPassCause,StatusDiscard) " +
            "value (#{checkin.CheckinCode},#{checkin.ElderlyCode},#{checkin.ElderlyName}," +
            "#{checkin.CheckinSetmealCode},#{checkin.CheckinSetmealName},#{checkin.BerthCode},#{checkin.BerthNumber},#{checkin.DurationPlan}," +
            "#{checkin.MoneyPlan},#{checkin.BeginTime},#{checkin.EndTime},#{checkin.CreateTime},#{checkin.Type}," +
            "#{checkin.Status},#{checkin.StaffCode},#{checkin.InstitutionCode},#{checkin.ContractImg},#{checkin.ChargeType},#{checkin.CheckinAutomatchCode}," +
            "#{checkin.BeginTimePlan},#{checkin.EndTimePlan},#{checkin.Price},#{checkin.PriceMonth},#{checkin.PriceDeduction},#{checkin.PriceMonthDeduction}," +
            "#{checkin.FixedFee},#{checkin.FixedFeeDeductionStatus},#{checkin.NeedVerify},#{checkin.DeductionStartTime},#{checkin.DeductionEndTime}," +
            "#{checkin.PreferentialPrice},#{checkin.PreferentialEndTime},#{checkin.CutPrice},#{checkin.Remarks},#{checkin.AuditResult},#{checkin.NoPassCause},#{checkin.StatusDiscard})")
    int insert(@Param("checkin") Checkin checkin);

    @Update("update checkin set ElderlyCode = #{checkin.ElderlyCode},ElderlyName = #{checkin.ElderlyName},CheckinSetmealCode=#{checkin.CheckinSetmealCode},CheckinSetmealName=#{checkin.CheckinSetmealName}," +
            "BerthCode=#{checkin.BerthCode},BerthNumber=#{checkin.BerthNumber},DurationPlan=#{checkin.DurationPlan},MoneyPlan=#{checkin.MoneyPlan},BeginTime=#{checkin.BeginTime}," +
            "EndTime=#{checkin.EndTime},CreateTime=#{checkin.CreateTime},Type=#{checkin.Type},Status=#{checkin.Status}," +
            "StaffCode=#{checkin.StaffCode},InstitutionCode=#{checkin.InstitutionCode},ContractImg=#{checkin.ContractImg}," +
            "ChargeType=#{checkin.ChargeType},CheckinAutomatchCode=#{checkin.CheckinAutomatchCode},BeginTimePlan=#{checkin.BeginTimePlan},EndTimePlan=#{checkin.EndTimePlan},Price=#{checkin.Price}," +
            "PriceMonth=#{checkin.PriceMonth},PriceDeduction=#{checkin.PriceDeduction},PriceMonthDeduction=#{checkin.PriceMonthDeduction},FixedFee=#{checkin.FixedFee}," +
            "FixedFeeDeductionStatus=#{checkin.FixedFeeDeductionStatus},NeedVerify=#{checkin.NeedVerify},DeductionStartTime=#{checkin.DeductionStartTime},DeductionEndTime=#{checkin.DeductionEndTime}," +
            "PreferentialPrice=#{checkin.PreferentialPrice},PreferentialEndTime=#{checkin.PreferentialEndTime},CutPrice=#{checkin.CutPrice},Remarks=#{checkin.Remarks},AuditResult=#{checkin.AuditResult},NoPassCause=#{checkin.NoPassCause},StatusDiscard=#{checkin.StatusDiscard} " +
            "where CheckinCode=#{checkin.CheckinCode}")
    int updateByCheckinCode(@Param("checkin") Checkin checkin);

    @Delete("delete from checkin where CheckinCode=#{checkinCode}")
    int delete(@Param("checkinCode") String checkinCode);

    @Select("select * from checkin where CheckinCode=#{checkinCode} limit 1")
    Checkin getByCheckinCode(@Param("checkinCode") String checkinCode);

    @Select("select * from checkin where ElderlyCode=#{elderlyCode} and Status in (0,1) and InstitutionCode=#{institutionCode} and StatusDiscard=0 limit 1")
    Checkin getByElderlyCodeAndInstitutionCode(@Param("elderlyCode") String elderlyCode, @Param("institutionCode") String institutionCode);

    @Select("<script>" +
            "select * from checkin where InstitutionCode=#{institutionCode} and ChargeType =1  and StatusDiscard=0" +
            "<if test ='elderlyName !=null and elderlyName!=\"\" '>" +
            "and ElderlyName like concat('%',#{elderlyName},'%') " +
            "</if>" +
            "<if test ='startTime !=null and endTime!=null and startTime!=\"\" and endTime!=\"\" '> " +
            "and (BeginTime BETWEEN #{startTime} AND #{endTime}) " +
            "</if>" +
            "<if test ='status !=-1 '> " +
            "and Status = #{status} " +
            "</if>" +
            "order by Status" +
            "</script>")
    List<Checkin> getListByInstitutionCode(@Param("institutionCode")String institutionCode,@Param("elderlyName") String elderlyName,
                                           @Param("status") int status,@Param("startTime") String startTime,@Param("endTime") String endTime);
    @Select("select * from checkin where CheckinSetmealCode=#{checkinSetmealCode} and Status!=2 and StatusDiscard=0 order by CreateTime desc")
    List<Checkin> getByCheckinSetmealCodeAndStatus1(@Param("checkinSetmealCode") String checkinSetmealCode);

    @Select("select * from checkin where ElderlyCode=#{elderlyCode} and StatusDiscard=0 order by CreateTime desc")
    List<Checkin> getListByElderlyCode(@Param("elderlyCode") String elderlyCode);

    @Select("select * from checkin where BerthCode = #{berthCode} and Status =#{status} and StatusDiscard=0 limit 1")
    Checkin getByBerthCode(@Param("berthCode") String berthCode,@Param("status") int status);


}
